//
//  ZRRefreshHeader.m
//  TestFile
//
//  Created by 黄涛 on 2018/6/4.
//  Copyright © 2018年 アポロジャパン. All rights reserved.
//

#import "ZRRefreshHeader.h"
#import "ZRLoadingView.h"
/** 快速设置颜色 */
#define ZRColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]

#define ZRMainColor ZRColor(138, 198, 51)
@interface ZRRefreshHeader ()

/** 刷新Label */
@property (nonatomic , weak) UILabel * loadingLabel;

@property (nonatomic , weak) ZRLoadingView * loadingView;


@end
@implementation ZRRefreshHeader

#pragma mark - 初始配置
- (void)prepare{
    [super prepare];

    self.loadingLabel.hidden = NO;
    self.mj_h = 60;
}

#pragma mark - 初始设置子控件的位置和尺寸
- (void)placeSubviews{
    [super placeSubviews];
    
    CGFloat width = self.bounds.size.width;
    
    self.loadingView.style = fish;
    
    switch (self.loadingView.style) {
        case circle:
        {
            self.loadingLabel.frame = CGRectMake(0, self.mj_h - 20,width , 20);
            self.loadingView.frame = CGRectMake(0, 0, 50, 50);
            self.loadingView.centerX = self.centerX;
            
        }break;
            
        case fish:
        {
            self.loadingLabel.frame = CGRectMake(0, self.mj_h - 20,width , 20);
            self.loadingView.frame = CGRectMake(0, 0, ZRScreenW , self.mj_h);
            self.loadingView.centerX = self.centerX;
            self.loadingView.backgroundColor = [UIColor redColor];
            
            
        }break;
            
        default:
            break;
    }
    
    
    

}


#pragma mark - ----------------------- 生命周期 -----------------------
#pragma mark - ----------------------- 其他方法 -----------------------
#pragma mark - ----------------------- 代理方法 -----------------------
#pragma mark - ----------------------- 活动监听 -----------------------
#pragma mark - content offset 改变
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change{
    [super scrollViewContentOffsetDidChange:change];
    
    if (self.scrollView.contentOffset.y > self.scrollView.contentInset.top || self.state == MJRefreshStateRefreshing) return;

    //正在拖动
    if (_scrollView.isDragging) {

        self.loadingLabel.hidden = YES;
        self.loadingView.progress = ((fabs(_scrollView.contentOffset.y) - 40 - _scrollView.contentInset.top)) / self.mj_h ;

        self.state = fabs(_scrollView.contentOffset.y)>= self.mj_h + _scrollView.contentInset.top ? MJRefreshStatePulling : MJRefreshStateIdle;

        return;
    }


    if (self.state == MJRefreshStateRefreshing ) return;

    self.state = (self.state == MJRefreshStatePulling) ? MJRefreshStateRefreshing : MJRefreshStateIdle;

    if (self.state == MJRefreshStateIdle) {
        self.loadingView.progress = (fabs(_scrollView.contentOffset.y) - 40 - _scrollView.contentInset.top) / self.mj_h;
    }
    
}

#pragma mark - content Size 改变
- (void)scrollViewContentSizeDidChange:(NSDictionary *)change{
    [super scrollViewContentSizeDidChange:change];
    
}

#pragma mark - 拖拽状态 改变
- (void)scrollViewPanStateDidChange:(NSDictionary *)change{
    [super scrollViewPanStateDidChange:change];
}

#pragma mark - ---------------------- set & get ----------------------

#pragma mark - loading Label
- (UILabel *)loadingLabel{
    if (!_loadingLabel) {
        
        UILabel * label = [[UILabel alloc] init];
        label.font = [UIFont systemFontOfSize:12];
        label.text = @"玩命加载中 。。。";
        label.textColor = ZRMainColor;
        label.textAlignment = NSTextAlignmentCenter;
        [self addSubview:label];
        
        _loadingLabel = label;
    }
    return _loadingLabel;
}

- (ZRLoadingView *)loadingView{
    if (!_loadingView) {
        ZRLoadingView * view = [[ZRLoadingView alloc] init];
        [self addSubview:view];
        _loadingView = view;
    }
    return _loadingView;
}




#pragma mark - 控件刷新状态
- (void)setState:(MJRefreshState)state{
    
    MJRefreshCheckState;
    
    switch (state) {
        case MJRefreshStateIdle:{ [self.loadingView stopAnimation];}break;

        case MJRefreshStatePulling:{}break;

        case MJRefreshStateRefreshing:
        {
            [self.loadingView startAnimation];
            self.loadingLabel.hidden = NO;
            [_scrollView.mj_footer endRefreshing];

        }break;

        default:    break;
    }
    
}

#pragma mark - 拖拽比例
- (void)setPullingPercent:(CGFloat)pullingPercent{
    [super setPullingPercent:pullingPercent];
    
}


@end
